package com.jp.knowledge.dialog;

import android.app.Dialog;
import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.jp.knowledge.R;
import com.jp.knowledge.my.util.MyUtil;

/**
 * Created by 528787067@qq.com on 2017/6/28.
 */

public class InfoDetailCommentDialog extends Dialog implements View.OnClickListener {

	private Context mContext;
	private View contentView;
	private TextView cancelBtn;
	private TextView sendBtn;
	private TextView sendRemindTv;
	private EditText editContent;

	public InfoDetailCommentDialog(Context context) {
		super(context, R.style.custom_idalgo);
		this.mContext = context;
		this.contentView = View.inflate(context, R.layout.dialog_info_detail_comment, null);
		cancelBtn = (TextView) contentView.findViewById(R.id.cancel_btn);
		sendBtn = (TextView) contentView.findViewById(R.id.send_btn);
		sendRemindTv = (TextView) contentView.findViewById(R.id.send_remind_tv);
		editContent = (EditText) contentView.findViewById(R.id.edit_content);
		cancelBtn.setOnClickListener(this);
		sendBtn.setOnClickListener(this);
		editContent.addTextChangedListener(new TextWatcher() {
			@Override
			public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
			@Override
			public void onTextChanged(CharSequence s, int start, int before, int count) {}
			@Override
			public void afterTextChanged(Editable s) {
				if(s.toString().trim().length() == 0) {
					sendBtn.setEnabled(false);
					sendBtn.setTextColor(mContext.getResources().getColor(R.color.font_gray_think));
				} else {
					sendBtn.setEnabled(true);
					sendBtn.setTextColor(mContext.getResources().getColor(R.color.font_black_think));
				}
			}
		});
		setContentView(contentView);
		getWindow().setGravity( Gravity.BOTTOM);
		getWindow().getAttributes().width = context.getResources().getDisplayMetrics().widthPixels;
	}

	public void setEditContent(String content){
		editContent.setText(content);
	}

	public String getEditContent(){
		return String.valueOf(editContent.getText());
	}

	public void clearEditContent(){
		setEditContent("");
	}

	public void setEditHint(String hint){
		editContent.setHint(hint);
	}

	public String getEditHint(){
		return String.valueOf(editContent.getHint());
	}

	@Override
	public void show() {
		super.show();
		editContent.postDelayed(new Runnable() {
			@Override
			public void run() {
				MyUtil.startKeyword(mContext, editContent);
			}
		}, 200);
	}

	@Override
	public void dismiss() {
		MyUtil.closeKeyword(mContext, editContent);
		editContent.postDelayed(new Runnable() {
			@Override
			public void run() {
				InfoDetailCommentDialog.super.dismiss();
			}
		}, 200);
	}

	@Override
	public void onClick(View v) {
		if(v == cancelBtn)
			dismiss();
		else if(v == sendBtn && onSendBtnClickListener != null)
			onSendBtnClickListener.onSendClick();
	}

	private OnSendBtnClickListener onSendBtnClickListener;
	public void setOnSendBtnClickListener(OnSendBtnClickListener onSendBtnClickListener){
		this.onSendBtnClickListener = onSendBtnClickListener;
	}
	public interface OnSendBtnClickListener{
		void onSendClick();
	}
}
